package kr.co.mcshop.review;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import kr.co.mcshop.qna.QnA;
import kr.co.mcshop.qnareply.QnAReply;
import kr.co.mcshop.reviewComment.ReviewComment;
import kr.co.mcshop.reviewComment.ReviewCommentService;
import kr.co.mcshop.reviewreply.ReviewReply;
import kr.co.mcshop.reviewreply.ReviewReplyService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application home page.
 */

@Controller
public class ReviewHomeController {

	private static final Logger logger = LoggerFactory
			.getLogger(ReviewHomeController.class);

	@Autowired
	private ReviewBoardService board;
	@Autowired
	private ReviewCommentService comment;
	@Autowired
	private ReviewReplyService reply;
	
	@Value("${file.upload.path}")
	private String fileUploadPath;

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/ReviewList", method = RequestMethod.GET)
	public String ReviewList(Locale locale, Model model, @RequestParam Map map) {

		List<ReviewBoard> BOARD = board.getAllBoard(map);

		model.addAttribute("BOARD", BOARD);

		return "review/ReviewList";
	}

	@RequestMapping(value = "/aaaaa")
	public String aaaaa(HttpServletRequest request) {
		return "/aaaaa";

	}

	/* 게시글 상세화면 */
	@RequestMapping(value = "/ReviewDetailOne", method = RequestMethod.GET)
	public String ReviewDetailOne(Locale locale, Model model,
			@RequestParam Map map, HttpServletRequest request) {
		board.updateCount(map);
		List<ReviewBoard> BOARD = board.getAllBoard(map);
		List<ReviewComment> COMMENT = comment.getAllComment(map);
		ArrayList<ReviewReply> Reply = new ArrayList<ReviewReply>();

		for (int i = 0; i < COMMENT.size(); i++) {
			//System.out.println("selectReply" + qnareplyService.selectreply(list1.get(i).getComment_no()));
			Reply.addAll(reply.selectreply(COMMENT.get(i).getComment_no()));
		}
		model.addAttribute("BOARD", BOARD.get(0));
		model.addAttribute("COMMENT", COMMENT);
		model.addAttribute("ReviewReply", Reply);
		return "/review/ReviewDetailOne";

	}

	/* 게시글입력 */
	@RequestMapping(value = "ReviewInsertForm", method = RequestMethod.GET)
	public String ReviewInsertForm() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("ReviewInsertForm");
		return "review/ReviewInsertForm";

	}

	@RequestMapping(value = "ReviewInsert", method = RequestMethod.POST)
	public String ReviewInsert(@RequestParam(value = "file") MultipartFile file, Locale locale,
			Model model, @RequestParam Map map) throws IllegalStateException,
			IOException {
		if(file.getOriginalFilename()==""){
			map.put("image", "");
		}else{
			String image = file.getOriginalFilename();
			File newFile = new File(fileUploadPath + file.getOriginalFilename());
			file.transferTo(newFile);	
		}
		
		board.insert(map);
		return "redirect:/ReviewList";

	}

	/* 게시글수정 */
	@RequestMapping(value = "ReviewUpdateForm", method = RequestMethod.GET)
	public String ReviewUpdateForm(Locale locale, Model model,
			@RequestParam Map map, HttpServletRequest request) {
		List<ReviewBoard> BOARD = board.getAllBoard(map);
		model.addAttribute("BOARD", BOARD.get(0));
		return "/review/ReviewUpdateForm";
	}

	@RequestMapping(value = "/ReviewUpdate", method = RequestMethod.POST)
	public String ReviewUpdate(@RequestParam(value = "file") MultipartFile file, Locale locale,
			Model model, @RequestParam Map map) throws IllegalStateException, IOException {
		
		String image = file.getOriginalFilename();
		File newFile = new File(fileUploadPath + file.getOriginalFilename());
		file.transferTo(newFile);
			 
			
		map.put("image", image);
		
		board.update(map);

		return "redirect:/ReviewList";
	}

	@RequestMapping(value = "ReviewCommentInsert", method = RequestMethod.POST)
	public String ReviewReplyInsert(Locale locale, Model model,
			@RequestParam Map map) {
		comment.insertComment(map);
		return "redirect:/ReviewList";
	}
	
	/* 게시글 삭제 
	@RequestMapping(value = "ReviewDeleteForm", method = RequestMethod.GET)
	public String ReviewDeleteForm(Locale locale, Model model,
			@RequestParam Map map, HttpServletRequest request) {
		List<ReviewBoard> BOARD = board.getAllBoard(map);
		model.addAttribute("BOARD", BOARD.get(0));
		return "/review/ReviewDeleteForm";
	}
*/
	@RequestMapping(value = "ReviewDelete", method = RequestMethod.GET)
	public String ReviewDelete(@RequestParam Map map) {
		board.delete(map);
		return "redirect:/ReviewList";
	}

	
	@RequestMapping(value="deleteComment")
	public String deleteComment(@RequestParam Map map){
		comment.deleteComment(map);
		return "redirect:/ReviewList";
	}
	
	@RequestMapping(value = "/search")
	   public String search(String board_title, Model model, @RequestParam Map map) {
	       ArrayList<ReviewBoard> list = new ArrayList<ReviewBoard>();
	       System.out.println("2222"+map);
	      if (map.get("searchtxt") == null) {
	         return "redirect:/ReviewList";
	      }
	      if(map.get("search").equals("0")){
	         map.put("searchtxt", map.get("searchtxt"));
	         List<ReviewBoard> titleSearchResult = board.searchtitle(map);
	         
	         for(ReviewBoard qnalist : titleSearchResult){
	            list.add(qnalist);
	         }
	      }else if(map.get("search").equals("1")){
	         map.put("searchtxt", map.get("searchtxt"));
	         List<ReviewBoard> userIdSearchResult = board.searchuserid(map);
	         
	         for(ReviewBoard qnalist : userIdSearchResult){
	            list.add(qnalist);
	         }
	      }
	      
	      model.addAttribute("BOARD", list);
	      return "review/ReviewList";
	}
	
	
	@RequestMapping(value = "/ReviewCommentUpdate", method = RequestMethod.POST)
	public String ReviewCommentUpdate(Locale locale, Model model, @RequestParam Map map) {
		System.out.println(map);
		comment.updatereviewcomment(map);
		return "redirect:/ReviewDetailOne?posts_no="+map.get("posts_no");
	}
	
	
	
	
	
	
	
	@RequestMapping(value = "/ReviewReplyInsert", method = RequestMethod.POST)
	public String replyInsert(Model model, @RequestParam Map map) {
		System.out.println(map);
		reply.replyInsert(map);
		return "redirect:/ReviewDetailOne?posts_no="+map.get("posts_no");
	}

	@RequestMapping(value = "/ReviewReplyUpdate", method = RequestMethod.POST)
	public String qnareplyupdate(Locale locale, Model model, @RequestParam Map map) {
		System.out.println(map);
		reply.updateqnareply(map);
		return "redirect:/ReviewDetailOne?posts_no="+map.get("posts_no");
	}
	
	@RequestMapping(value = "/ReviewReplyDelete", method = RequestMethod.GET)
	public String qnareplyDelete(Locale locale, Model model, @RequestParam Map map) {
		System.out.println(map);
		reply.qnareplyDelete(map);
		return "redirect:/ReviewDetailOne?posts_no="+map.get("posts_no");
	}
	@RequestMapping(value = "text")
	public String text() {
		return "text";
	}
	
	
	
	
	
}
